<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="./Public/Admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="./Public/Admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <link href="./Public/Admin/Flat/docs/assets/css/demo.css" rel="stylesheet">
	    <link rel="shortcut icon" href="./Public/Admin/Flat/img/favicon.ico">
	    <!-- HTML5 shim, for IE6-8 support of HTML5 elements. All other JS at the end of file. -->
	    <!--[if lt IE 9]>
	      <script src="dist/js/vendor/html5shiv.js"></script>
	      <script src="dist/js/vendor/respond.min.js"></script>
	    <![endif]-->
	    <script src="./Public/jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<table class="table table-hover">
			<tr class="active">
				<td width="80">货品id</td>

				<?php foreach ($specData as $k=>$v){?>
				<td width=""><?php echo $v['taname']?></td>
				<?php }?>

				<td width="40">货号</td>
				<td width="">库存</td>
				<td width="150">操作</td>
			</tr>
			
			
			
			<div class="dataTr">
			<?php foreach ($goodsListData as $k=>$v){?>
			<tr>
				<td>
					<?php echo $v['glid']?>
					<input type="hidden" name="glid" value="<?php echo $v['glid']?>" />
				</td>
				
				<?php foreach ($v['combine'] as $kk=>$vv){?>
				<td><?php echo $vv?></td>
				<?php }?>
					<input type="hidden" name="number" id="number" value="<?php echo $v['number']?>" />
					<input type="hidden" name="inventory" id="inventory" value="<?php echo $v['inventory']?>" />
				<td><?php echo $v['number']?></td>
				<td><?php echo $v['inventory']?></td>
				<td>
					<a href="javascirpt:;" class="edit btn btn-sm btn-warning">编辑</a>
					<a href="javascript: if(confirm('你确定要删除么？')) location.href='<?php echo U("del",array("glid" => $v["glid"],"gid" => $gid))?>';" class="btn btn-sm btn-danger">删除</a>
				</td>
			</tr>
			
			
			<?php }?>
			</div>
			<!--编辑-->
			<script type="text/javascript">
				$(function(){
					var bigTr = "";
					var numIndex = "";
//					货号
					var huohaoI = "";
					$('.edit').live('click',function(){
						var numIndex = $(this).index('.edit');
						bigTr = $(this).parents('tr').html();
//						alert(bigTr);
						var tdSize = $(this).parents('tr').find('td').size();
						var trHtml =$(this).parents('tr').html();
						huohaoI = $(this).parents('tr').find('td').eq(0).text();
						var inventorys = $(this).parents('tr').find('td').eq(tdSize - 2).text();
//						alert(inventorys);return;
						var numbers = $(this).parents('tr').find('td').eq(tdSize - 3).text();
						var inventorysStr = '<input type="text" name="number" id="numberJquery" value="' + inventorys + '" />';
						var numbersStr = '<input type="text" name="inventory" id="inventoryJquery" value="' + numbers + '" />';
						$(this).parents('tr').find('td').eq(tdSize - 2).html(inventorysStr);
						$(this).parents('tr').find('td').eq(tdSize - 3).html(numbersStr);
//						$('#inventory').attr('type','text');
//						alert(numbersStr);
//						将编辑按钮变为input submit 按钮
						$(this).parents('tr').find('a:eq(0)').removeClass('edit');
						$(this).parents('tr').find('a:eq(0)').addClass('xiugai');
						$(this).parents('tr').find('a:eq(0)').text('修改');
						$(this).parents('tr').find('a:eq(1)').attr('href','javascript:;');
						$(this).parents('tr').find('a:eq(1)').attr('id','quxiao');
						$(this).parents('tr').find('a:eq(1)').text('取消');
					})
					$('#quxiao').live('click',function(){
						$(this).parents('tr').html(bigTr);
					})
//					传ajax
					$('.xiugai').live('click',function(){
//						获取修改的值
						var numberSe = $('#numberJquery').attr('value');
						var inventorySe = $('#inventoryJquery').attr('value');
						var gid = $('#gid').attr('value');
//						alert(numberSe);
//						alert(inventorySe);
						$.ajax({
							type:"post",
							url:"<?php echo U('edit')?>",
							data:{gid:gid,huohaoI:huohaoI,numberSe:numberSe,inventorySe:inventorySe},
							dataType:"Json",
							success:function(phpData){
								if(phpData == 1){
									alert('修改成功');
									location.href = "<?php echo U('index',array('gid' => $gid))?>";
								}
							}
						});
						
						
						
						
						
						
					})
					
				})
			</script>
			<!--添加货品-->
			<form action="<?php echo U('add')?>" method="post">
			<tr>
				<td>添加货品</td>
				<?php foreach ($specData as $k=>$v){?>
				<td width="40">
					<select class="combine" name="spec[]">
						<option value="">请选择</option>
						<?php foreach ($v['sonData'] as $kk=>$vv){?>
						<option value="<?php echo $vv['gtid']?>"><?php echo $vv['gtvalue']?></option>
						<?php }?>
					</select>
				</td>
				<?php }?>
				<!--库存-->
				<td width="">
					<input type="text" name="number" id="" value="" />
				</td>
				<!--货号-->
				<td width="40">
					<input type="text" name="inventory" id="" value="" />
				</td>
				<!--操作-->
				<td>
					<input type="hidden" name="snshop_goods_gid" id="gid" value="<?php echo $gid?>" />
					<input class="btn btn-sm btn-warning" type="submit" value="确认添加" />
					<a href="<?php echo U('Goods/index')?>" class="btn btn-sm btn-danger">返回</a>
				</td>
				
			</tr>
			</form>
			<!--鉴定是否已经添加过-->
			<script type="text/javascript">
					$(".combine").change(function(){
//						获取被选中的option
//						获取商品的主键
						var gid = $('input[name=snshop_goods_gid]').attr('value');
						var combine = $('.combine option:selected').text();
						var size = $('#h_size option:selected').text();
						
						var combineStr = "";
						$.each($('.combine'), function() {
							combineStr += $(this).find('option:selected').attr('value') + ",";
						});
//						alert(combineStr);
							
//						alert(gid); return;
//						alert(size);
						$.ajax({
						url:"<?php echo U('jiansun')?>",
						type:"post",
						data:{combineStr:combineStr,gid:gid},
						dataType:'json',
						success:function(phpData){
							if(phpData.combine){
								alert('货品已经存在，如果要修改库存，请点击修改');
//								清空添加表
								$.each($('.combine'), function() {
									$(this).find('option').eq(0).attr('selected','selected');
								});
							}
						}
					});
					return false;
						
						
						
						
					})
				</script>
		</table>
	</body>
</html>
